<?php
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
$tblname = $_REQUEST["tblname"];
$payment_method = $_REQUEST["payment_method"];

if ($tblname == "")
    $tblname = "tbl_pet";
$db = NEW myDBS();

if ($payment_method == "")
    $payment_method = "เช็ค";
//$payment_method = "";
switch ($tblname) {
    case "tbl_pet":
        $sql = "SELECT DISTINCT tbl_pet.*, expenses2_name, expenses3_name, panngan2_name, panngan3_name, vender_name, status  "
                . " FROM panngan2, panngan3, v_vender, tbl_pet "
                . " LEFT JOIN expenses_lvl2 ON ex2_id = expenses_lvl2.expenses2_id "
                . " LEFT JOIN expenses_lvl3 ON ex3_id = expenses_lvl3.expenses3_id "
//                . " LEFT JOIN tbl_pay ON tbl_pet.pet_id = tbl_pay.pet_id "
                . " WHERE approved_status = 'อนุมัติ' "
                . " AND payment_method IN ('$payment_method', '')"
                . " AND panngan2.panngan2_id = tbl_pet.panngan2_id "
                . " AND panngan3.panngan3_id = tbl_pet.panngan3_id "
                . " AND tbl_pet.receiver = v_vender.vender_id "
                . " ORDER BY tbl_pet.id DESC";

//        $sql = "SELECT DISTINCT pet_id, expenses1_name, expenses2_name, net, cheque_no,  "
//                . " FROM tbl_pet, expenses_lvl1, expenses_lvl2 "
//                . " WHERE approved_status = 'อนุมัติ' "
//                . " AND expenses_lvl1.expenses1_id = ex1_id "
//                . " AND expenses_lvl2.expenses2_id = ex2_id ";

        break;

    case "tbl_petout":

        $sql = "SELECT DISTINCT tbl_petout.*, panngan2_name, panngan3_name "
                . " , money_out2_name, money_out3_name, vender_name "
                . " FROM panngan2, panngan3, money_out2, tbl_petout "
                . " LEFT JOIN money_out3 ON tbl_petout.money_out3_id = money_out3.money_out3_id "
                . " LEFT JOIN v_vender ON tbl_petout.vender_id = v_vender.vender_id "
                . " WHERE approved_status = 'อนุมัติ' "
                . " AND payment_method IN ('$payment_method', '')"
                . " AND tbl_petout.panngan2_id = panngan2.panngan2_id "
                . " AND tbl_petout.panngan3_id = panngan3.panngan3_id "
                . " AND tbl_petout.money_out2_id = money_out2.money_out2_id "
                . " ORDER BY tbl_petout.id DESC";
        break;

    case "tbl_petition":

        $sql = "SELECT DISTINCT p_id AS pet_id, expenses1_name, expenses2_name, inpNet AS net, cheque_no "
                . " FROM tbl_petition "
                . " LEFT JOIN expenses_lvl1 ON ex1_id = expenses_lvl1.expenses1_id "
                . " LEFT JOIN expenses_lvl2 ON ex2_id = expenses_lvl2.expenses2_id "
                . " WHERE payment_method =  'cheque' ";

        break;

    case "tbl_salary":
        $sql = "SELECT pet_id, expenses1_name, expenses2_name, cheque_no, sum(salary) AS net "
                . " FROM tbl_salary, tbl_payment, expenses_lvl1, expenses_lvl2 "
                . " WHERE approved_status = 'อนุมัติ' "
                . " AND tbl_salary.id = tbl_payment.card_id "
                . " AND payment_method = 'cheque' "
                . " AND expenses_lvl1.expenses1_id = ex1_id "
                . " AND expenses_lvl2.expenses2_id = ex2_id "
                . " GROUP BY pet_id, expenses1_name, expenses2_name, cheque_no ";

        break;

    case "tbl_welfare":
        $sql = "SELECT pet_id, expenses1_name, expenses2_name, cheque_no, sum(welfare) AS net "
                . " FROM tbl_welfare, tbl_payment, expenses_lvl1, expenses_lvl2 "
                . " WHERE approved_status = 'อนุมัติ' "
                . " AND tbl_welfare.id = tbl_payment.card_id "
                . " AND payment_method = 'cheque' "
                . " AND expenses_lvl1.expenses1_id = ex1_id "
                . " AND expenses_lvl2.expenses2_id = ex2_id "
                . " GROUP BY pet_id, expenses1_name, expenses2_name, cheque_no ";

        break;

    default:
//        $sql = "SHOW TABLES";
        $sql = "SELECT DISTINCT pet_id, expenses1_name, expenses2_name, net, cheque_no  "
                . " FROM tbl_pet, expenses_lvl1, expenses_lvl2 "
                . " WHERE payment_method = 'cheque' "
                . " AND expenses_lvl1.expenses1_id = ex1_id "
                . " AND expenses_lvl2.expenses2_id = ex2_id ";

        break;
}
//$sql = "SELECT DISTINCT pet_id, expenses1_name, expenses2_name, net "
//        . " FROM tbl_pet, expenses_lvl1, expenses_lvl2 "
//        . " WHERE payment_method = 'cheque' "
//        . " AND expenses_lvl1.expenses1_id = ex1_id "
//        . " AND expenses_lvl2.expenses2_id = ex2_id "
//        . " UNION ALL "
//        . "SELECT DISTINCT p_id AS pet_id , expenses1_name, expenses2_name, inpNet AS net"
//        . " FROM tbl_petition, expenses_lvl1, expenses_lvl2 "
//        . " WHERE payment_method = 'cheque' "
//        . " AND expenses_lvl1.expenses1_id = ex1_id "
//        . " AND expenses_lvl2.expenses2_id = ex2_id ";
//echo $sql;
$result = $db->runSQL($sql);
?>

<script type="text/javascript">
    $(document).ready(function() {
        $('#petition').dataTable();

        $("#tblname").change(function() {
            //alert($(this).val());
            window.location.href = "index.php?pages=cheque_new&tblname=" + $(this).val();
        });

        $("input:radio[name=payment_method]").click(function() {
            //alert($(this).val());
            var tblname = $("#tblname").val();
            var payment_method = $(this).val();
            window.location.href = "index.php?pages=cheque_new&tblname=" + tblname + "&payment_method=" + payment_method;
        });

    });

    function printCheque(id) {
        var val = [];
        var count = 0;
        var tblname = "<?php echo $tblname; ?>";
        var payment_method = "<?php echo $payment_method; ?>";

        $(':checkbox:checked').each(function(i) {
            val[i] = $(this).val();
            count++;
            //alert(val[i]);
        });

        if (count == 0) {
            alert("กรุณาเลือกข้อมูลที่ต้องการบันทึกด้วยครับ");
        } else {
            if (payment_method == "เงินสด") {
                $.ajax({
                    type: "POST",
                    url: "pay_process",
                    data: {
                        val: val
                        , payment_method: payment_method
                                //, i_date: i_date
                    },
                    cach: false,
                    error: function() {
                        alert("ERROR !!!!");
                    },
                    success: function(data) {
//                        $("#test").html(data);

                        window.location.href = "index.php?pages=cheque_new";
                    }
                });
            } else {
                window.location.href = "index.php?pages=cheque_save&tblname=" + tblname + "&val=" + val + "&payment_method=" + payment_method;
            }


//            $('<div>').dialog2({
//                autoOpen: true,
//                title: "พิมพ์เช็ค",
//                content: "dlg_cheque.php?val=" + val + "&tblname=" + tblname,
//                id: "modal-print"
//            });
        }
    }
</script>

<div id='test'></div>
<div class="control-group">
    <div class="controls-row">
        <label>เลือกวิธีการจ่ายเงิน</label>
        <label class="radio inline">
            <input type="radio" id="payment_method" name="payment_method" value="เช็ค" <?= ($payment_method == "เช็ค") ? "checked" : "" ?>> เช็ค
        </label>
        <label class="radio inline">
            <input type="radio" id="payment_method" name="payment_method" value="โอนเงิน" <?= ($payment_method == "โอนเงิน") ? "checked" : "" ?>> โอนเงิน
        </label>
        <label class="radio inline">
            <input type="radio" id="payment_method" name="payment_method" value="เงินสด" <?= ($payment_method == "เงินสด") ? "checked" : "" ?>> เงินสด
        </label>
        <br>
        <select id="tblname" name="tblname" >
            <option value="">เลือก...</option>
            <option value="tbl_pet" <?php echo ($tblname == "tbl_pet") ? "selected" : ""; ?>>ฎีกาเงินในงบประมาณ</option>
            <option value="tbl_petout" <?php echo ($tblname == "tbl_petout") ? "selected" : ""; ?>>ฎีกาเงินนอกงบประมาณ</option>
            <option value="tbl_petition" <?php echo ($tblname == "tbl_petition") ? "selected" : ""; ?>>โครงการ</option>
            <option value="tbl_salary" <?php echo ($tblname == "tbl_salary") ? "selected" : ""; ?>>เงินเดือน</option>
            <option value="tbl_welfare" <?php echo ($tblname == "tbl_welfare") ? "selected" : ""; ?>>สวัสดิการ</option>
        </select>
        <a href="javascript:printCheque()" class=" btn btn-primary" target="_blank"><i class="icon-print icon-white"></i> บันทึกเช็ค</a>
    </div>
</div>


<?php
if ($tblname == "tbl_pet") {
    ?>
    <table id="petition" class="table table-bordered table-hover" width="100%" >
        <caption><br></caption>
        <thead>
            <tr>
                <th>#</th>
                <th>วันที่</th>
                <!--<th>เลขที่ฎีกา</th>-->
                <th>เลขที่คลังรับ</th>
                <!--<th>แผนงาน</th>-->
                <th>งาน</th>
                <th>หมวด</th>
                <th>ประเภท</th>
                <th>ผู้รับเงิน</th>
                <th>จำนวนเงิน</th>
                <th>ดู</th>
                <th>action</th>

            </tr>
        </thead>
        <tbody>
            <?php
            $i = 1;
            while ($rows = $db->fetch_object($result)) {
                ?>
                <tr>
                    <td><p class="text-center"><?php echo $i; ?></td>
                    <td><p class="text-center"><?php echo $rows->approved_date; ?></p></td>
                    <td><p class="text-center"><?php echo $rows->finance_id; ?></p></td>
                    <td>&nbsp;<?php echo $rows->panngan3_name; ?></td>
                    <td>&nbsp;<?php echo $rows->expenses2_name; ?></td>
                    <td>&nbsp;<?php echo $rows->expenses3_name; ?></td>
                    <td>&nbsp;<?php echo $rows->vender_name; ?></td>
                    <td><p class="text-right">&nbsp;<?php echo number_format($rows->net, 2); ?></p></td>
                    <td><p class='text-center'><a href='pet_print.php?id=<?php echo $rows->id; ?>' target='_blank'><i class='icon-eye-open'></i></a></p></td>
                    <?php
//                in( 'จัดทำเช็ค', 'เช็คอนุมัติ', 'จ่ายเช็คแล้ว')
                    if ($rows->status != "จัดทำเช็ค" && $rows->status != "เช็คอนุมัติ" && $rows->status != "จ่ายเช็คแล้ว" && $rows->status != "จัดทำโอนเงิน" && $rows->status != "จัดทำเงินสด") {
                        ?>
                        <td><p class="text-center">&nbsp;<input type='checkbox' name='check[]' id='check_<?php echo $rows->pet_id; ?>' value='<?php echo $rows->pet_id; ?>' /></p></td>
                        <?php
                    } else {
                        ?>
                        <td><p class="text-center">&nbsp;ทำรายการแล้ว</p></td>
                        <?php
                    }
                    ?>

                </tr>
                <?php
                $i++;
            }
            ?>
        </tbody>
    </table>
    <?php
} elseif ($tblname == "tbl_petout") {
    ?>
    <table id="petition" class="table table-bordered table-hover" width="100%" >
        <caption><br></caption>
        <thead>
            <tr>
                <th>#</th>
                <th>วันที่</th>
                <!--<th>เลขที่ฎีกา</th>-->
                <th>เลขที่คลังรับ</th>
                <!--<th>แผนงาน</th>-->
                <th>งาน</th>
                <th>หมวด</th>
                <th>ประเภท</th>
                <th>ผู้รับเงิน</th>
                <th>จำนวนเงิน</th>
                <th>ดู</th>
                <th>action</th>

            </tr>
        </thead>
        <tbody>
            <?php
            $i = 1;
            while ($rows = $db->fetch_object($result)) {
                ?>
                <tr>
                    <td><p class="text-center"><?php echo $i; ?></td>
                    <td><p class="text-center"><?php echo $rows->approved_date; ?></p></td>
                    <td><p class="text-center"><?php echo $rows->finance_id; ?></p></td>
                    <td><?php echo $rows->panngan3_name; ?></td>
                    <td><?php echo $rows->money_out2_name; ?></td>
                    <td><?php echo $rows->money_out3_name; ?></td>
                    <td><p class="text-left"><?php echo $rows->vender_name; ?></p></td>
                    <td><p class="text-right"><?php echo number_format($rows->amount, 2); ?></p></td>
                    <td><p class='text-center'><a href='petout_print.php?id=<?php echo $rows->id; ?>' target='_blank'><i class='icon-eye-open'></i></a></p></td>
                    <?php
//                in( 'จัดทำเช็ค', 'เช็คอนุมัติ', 'จ่ายเช็คแล้ว')
                    if ($rows->status != "จัดทำเช็ค" && $rows->status != "เช็คอนุมัติ" && $rows->status != "จ่ายเช็คแล้ว" && $rows->status != "จัดทำโอนเงิน" && $rows->status != "จัดทำเงินสด") {
                        ?>
                        <td><p class="text-center">&nbsp;<input type='checkbox' name='check[]' id='check_<?php echo $rows->petout_id; ?>' value='<?php echo $rows->petout_id; ?>' /></p></td>
                        <?php
                    } else {
                        ?>
                        <td><p class="text-center">&nbsp;ทำรายการแล้ว</p></td>
                        <?php
                    }
                    ?>

                </tr>
                <?php
                $i++;
            }
            ?>
        </tbody>
    </table>
    <?php
}
?>
<center><a class="btn btn-info" href="rpt_cheque_1.php" target="_blank">พิมพ์รายงานจัดทำเช็ค</a></center>

